package org.hbgl.geoentity.db.service;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.codec.binary.Base64;
import org.hbgl.geoentity.db.util.ApiUrlConfig;
import org.hbgl.geoentity.db.util.HttpClient;
import org.hbgl.geoentity.db.util.JsonUtil;
import org.hbgl.geoentity.db.vo.BusinessLicense;
import org.hbgl.geoentity.db.vo.BusinessLicenseOCR;
import org.hbgl.geoentity.db.vo.ResponseObject;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

@Service
public class VerifyService {

    /**
     * 实名认证接口
     *
     * @param name   姓名
     * @param idcard 身份证号码
     */
    public ResponseObject verifyIdcardName(String name, String idcard) {
        String url = ApiUrlConfig.INTERFACE_URL + ApiUrlConfig.VERIFY_IDCARD_NAME;

        Map<String, String> params = new HashMap<>();
        params.put("name", name);
        params.put("idcard", idcard);

        return post(url, params);
    }

    /**
     * 企业三要素核验
     *
     * @param companyname 公司名称
     * @param creditno    统一社会信用代码
     * @param legalperson 法人姓名
     */
    public ResponseObject verifyFactor3Precise(String companyname, String creditno, String legalperson) {
        String url = ApiUrlConfig.INTERFACE_URL + ApiUrlConfig.VERIFY_FACTOR3_PRECISE;

        Map<String, String> params = new HashMap<>();
        params.put("companyname", companyname);
        params.put("creditno", creditno);
        params.put("legalperson", legalperson);

        return post(url, params);
    }

    /**
     * 营业执照验真
     *
     * @param keyword 公司全名或企业注册号
     */
    public ResponseObject verifyBusinessLicense(String keyword) {
        String url = ApiUrlConfig.INTERFACE_URL + ApiUrlConfig.VERIFY_BUSINESS_LICENSE;

        Map<String, String> params = new HashMap<>();
        params.put("keyword", keyword);

        return post(url, params);
    }

    /**
     * 营业执照OCR
     *
     * @param image 营业执照Base64图片
     */
    public ResponseObject verifyBusinessLicenseOcr(String image) {
        String url = ApiUrlConfig.INTERFACE_URL + ApiUrlConfig.VERIFY_BUSINESS_LICENSE_OCR;

        Map<String, String> params = new HashMap<>();
        params.put("image", image);

        return post(url, params);
    }

    /**
     * 调用接口
     *
     * @param url    接口地址
     * @param params 参数
     * @return
     */
    private ResponseObject post(String url, Map<String, String> params) {
        // 发送请求
        // String result = HttpClient.post(url, params);
        String result = HttpClient.postJson(url, JsonUtil.serialize(params));

        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            return mapper.readValue(result, ResponseObject.class);
        } catch (IOException e) {
            System.out.println(result);
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws Exception {
         ResponseObject responseObject = new VerifyService().verifyFactor3Precise("河北广联信息技术有限公司", "91130100794175098M", "王磊");
//         ResponseObject responseObject = new VerifyService().verifyIdcardName("王磊", "130105197510311537");
        if (responseObject.getCode() != 200) {
            System.out.println("企业三要素核验不通过：" + responseObject.getMsg());
        }
        if (responseObject.getData() == null || responseObject.getData().getResult() == null) {
            System.out.println("企业三要素核验不通过：结果异常");
        }
        if (!"1".equals(responseObject.getData().getResult().toString())) {
            System.out.println("公司名称：" + "河北广联信息技术有限公司");
            System.out.println("统一社会信用代码：" + "91130100794175098M");
            System.out.println("法人姓名：" + "王磊");

            System.out.println("企业三要素核验不通过：单位名称、统一社会信用代码、法定代表人姓名不匹配");
        }

//         ResponseObject responseObject1 = new VerifyService().verifyBusinessLicense("河北广联信息技术有限公司");
//        Object result1 = responseObject1.getData().getResult();
//        String serialize1 = JsonUtil.serialize(result1);
//        BusinessLicense businessLicense = JsonUtil.deserialize(serialize1, BusinessLicense.class);
//        System.out.println(JsonUtil.serialize(responseObject1));

        // ResponseObject responseObject = new ApiImpl().verifyFactor2Precise("test20220125", "惠安县崇武源建石业有限公司1");

        // String marginImageFile = "D:\\Users\\Administrator\\Downloads\\北京氢互动科技有限公司.jpg";
        // String marginImageFile = "D:\\Users\\Administrator\\Downloads\\杭州得小鲜品牌管理有限公司.jpg";

        String marginImageFile = "D:\\Users\\Administrator\\Downloads\\河北广联信息技术有限公司.png";



//        URL urlfile = new URL("http://123.59.199.9:8097/centerapi/public/storage/fetch/dwio2hcjf5j0osf90hka.jpg");
//        InputStream inStream = urlfile.openStream();
//        BufferedImage image= ImageIO.read(inStream);

//        String imageBase64 = Base64AndImage.imageToBase64String(image);
//        BufferedImage image = ImageIO.read(new FileInputStream(marginImageFile));
//        String imageBase64 = imageToBase64String(image);
//        ResponseObject responseObject = new VerifyService().verifyBusinessLicenseOcr(imageBase64);

//        Object result = responseObject.getData().getResult();
//        String serialize = JsonUtil.serialize(result);
//        BusinessLicenseOCR businessLicenseOCR = JsonUtil.deserialize(serialize, BusinessLicenseOCR.class);
        System.out.println(JsonUtil.serialize(responseObject));
//        BusinessLicenseOCR businessLicenseOCR = (BusinessLicenseOCR) responseObject.getData().getResult();

    }

    public static String imageToBase64String(BufferedImage image) {
        byte[] data = null;
        //读取图片字节数组
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, "gif", stream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        data = stream.toByteArray();
        //对字节数组Base64编码
        return new String(Base64.encodeBase64(data)).replaceAll("\r|\n", "");//返回Base64编码过的字节数组字符串
    }

}
